﻿using RecruitStudents.Client.BLL;
using RecruitStudents.Utility;
using RecruitStudents.Utility.Helper;
using RecruitStudents.Utility.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RecruitStudents.Client.Base
{
    public partial class Base_Pad_ExamScoreFiedSetting_List : BaseForm
    {
        ServiceBase service = new ServiceBase();
        public Base_Pad_ExamScoreFiedSetting_List()
        {
            InitializeComponent();
            BindFiledAll();
            BindFiledShow();
        }
        private void BindFiledAll()
        {
            using (DataTable dt = service.GetDicPadShowFiledAllTable("0"))
            {
                listFiledALL.DataSource = dt;         // 绑定数据源
                listFiledALL.DisplayMember = "DicName";  // 指定显示字段
                listFiledALL.ValueMember = "DicNO";    // 指定值字段
            }
        }

        private void BindFiledShow()
        {
            using (DataTable dt = service.GetDicPadShowFiledShowTable())
            {
                listFiledShow.DataSource = dt;         // 绑定数据源
                listFiledShow.DisplayMember = "DicName";  // 指定显示字段
                listFiledShow.ValueMember = "DicNO";    // 指定值字段
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                List<TB_Dic_PadFiledShow> list = new List<TB_Dic_PadFiledShow>();
                DevExpress.XtraEditors.BaseListBoxControl.SelectedItemCollection selectItems = listFiledALL.SelectedItems;
                for (int i = 0; i < selectItems.Count; i++)
                {
                    DataRowView dr = (DataRowView)selectItems[i];
                    TB_Dic_PadFiledShow model = new TB_Dic_PadFiledShow();
                    model.DicNO = dr["DicNO"].ToString();
                    model.DicName = dr["DicName"].ToString();
                    model.IsActive = "1";
                    list.Add(model);
                }
                service.SaveDicPadFiledShow(list);
                BindFiledAll();
                BindFiledShow();
            }
            catch (Exception ex)
            {
                DevHelper.ShowErrorMessageBox("操作数据出现错误:" + ex.Message);
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> list = new List<string>();
                DevExpress.XtraEditors.BaseListBoxControl.SelectedItemCollection selectItems = listFiledShow.SelectedItems;
                for (int i = 0; i < selectItems.Count; i++)
                {
                    DataRowView dr = (DataRowView)selectItems[i];
                    list.Add(dr["DicNO"].ToString());
                }
                service.DeleteDicPadFiledShow(list);
                BindFiledAll();
                BindFiledShow();
            }
            catch (Exception ex)
            {
                DevHelper.ShowErrorMessageBox("操作数据出现错误:" + ex.Message);
            }
        }

        private void btnUp_Click(object sender, EventArgs e)
        {
            int index = listFiledShow.SelectedIndex;
            if (index < 0)
            {
                return;
            }
            listFiledShow.SelectedIndex--;
            if (listFiledShow.SelectedIndex == -1)
            {
                return;
            }
            int indexNew = listFiledShow.SelectedIndex;
            object objValue = listFiledShow.GetItemValue(index);
            DataTable dt = (DataTable)listFiledShow.DataSource;
            DataRow drS = dt.Select("DicNO='" + objValue + "'")[0];
            object[] array = drS.ItemArray;
            dt.Rows.RemoveAt(index);
            DataRow drNew = dt.NewRow();
            drNew.ItemArray = array;
            dt.Rows.InsertAt(drNew, indexNew);

            listFiledShow.DataSource = dt;         // 绑定数据源
            listFiledShow.DisplayMember = "DicName";  // 指定显示字段
            listFiledShow.ValueMember = "DicNO";    // 指定值字段

            Dictionary<string, string> dic = new Dictionary<string, string>();
            for (int i = 0; i < listFiledShow.ItemCount; i++)
            {
                DataRowView dr = (DataRowView)listFiledShow.GetItem(i);
                dic.Add(dr["DicNO"].ToSafeString(), i.ToIntString());
            }
            service.SortDicPadFiledShow(dic);
            BindFiledShow();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            int index = listFiledShow.SelectedIndex;
            if (index < 0)
            {
                return;
            }
            listFiledShow.SelectedIndex++;
            int indexNew = listFiledShow.SelectedIndex;
            if (listFiledShow.SelectedIndex == -1)
            {
                return;
            }
            object objValue = listFiledShow.GetItemValue(index);
            DataTable dt = (DataTable)listFiledShow.DataSource;
            DataRow drS = dt.Select("DicNO='" + objValue + "'")[0];
            object[] array = drS.ItemArray;
            dt.Rows.RemoveAt(index);
            DataRow drNew = dt.NewRow();
            drNew.ItemArray = array;
            dt.Rows.InsertAt(drNew, indexNew);

            listFiledShow.DataSource = dt;         // 绑定数据源
            listFiledShow.DisplayMember = "DicName";  // 指定显示字段
            listFiledShow.ValueMember = "DicNO";    // 指定值字段

            Dictionary<string, string> dic = new Dictionary<string, string>();
            for (int i = 0; i < listFiledShow.ItemCount; i++)
            {
                DataRowView dr = (DataRowView)listFiledShow.GetItem(i);
                dic.Add(dr["DicNO"].ToSafeString(), i.ToIntString());
            }
            service.SortDicPadFiledShow(dic);
            BindFiledShow();
        }
    }
}
